package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.logic.bussiness.player.UserInteractiveBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.interactive.InteractiveRecord;
import com.yanqu.road.entity.interactive.UserInteractive;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 玩家交互，点赞、谣言
 */
public class InteractiveModule extends GeneralModule {

    private UserInteractive userInteractive;

    private LinkedList<InteractiveRecord> interactiveRecordList = new LinkedList<>();
    private List<Long> currLikeUserIdList = new ArrayList<>();
    private List<Long> currHateUserIdList = new ArrayList<>();

    public InteractiveModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean saveData() {
        if(null != userInteractive){
            if(userInteractive.isInsertOption()){
                UserInteractiveBussiness.addUserInteractive(userInteractive);
            }else if(userInteractive.isUpdateOption()){
                UserInteractiveBussiness.updateUserInteractive(userInteractive);
            }
        }
        List<InteractiveRecord> tempList = new ArrayList<>(interactiveRecordList);
        for(InteractiveRecord interactiveRecord : tempList){
            if(interactiveRecord.isInsertOption()){
                UserInteractiveBussiness.addInteractiveRecord(interactiveRecord);
            }else if(interactiveRecord.isUpdateOption()){
                UserInteractiveBussiness.updateInteractiveRecord(interactiveRecord);
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean loadData() {
        currLikeUserIdList = new ArrayList<>();
        currHateUserIdList = new ArrayList<>();
        userInteractive = UserInteractiveBussiness.getUserInteractive(player.getUserId());
        initUserInteractive();
        interactiveRecordList = UserInteractiveBussiness.getInteractiveRecordList(player.getUserId(), GameConfig.INTERACTIVE_RECORD_MAX_COUNT);
        return true;
    }

    @Override
    public void afterLogin() {
        syncUserInteractive();
    }

    @Override
    public void loginSendMsg() {

    }

    public void resetOneDay(){
        if(null == userInteractive){
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp()/1000;
        if(userInteractive.getModifyTime() < zeroTime){
            player.getUserInfo().setTodayBeHateTimes(0);
            userInteractive.setHateUserIdList(new ArrayList<>());
            userInteractive.setLikeUserIdList(new ArrayList<>());
            userInteractive.setModifyTime(zeroTime);
            syncUserInteractive();
        }
    }

    private void initUserInteractive(){
        if(null == userInteractive){
            UserInteractive interactive = new UserInteractive();
            interactive.setUserId(player.getUserId());
            interactive.setModifyTime(System.currentTimeMillis() / 1000);
            interactive.setInsertOption();
            userInteractive = interactive;
        }
    }

    public void syncUserInteractive(){
        PlayerProto.InteractiveDetailMsg.Builder msg = PlayerBasePb.parseInteractiveDetailMsg(userInteractive);
        if(null != msg){
            player.sendPacket(Protocol.U_INTERACTIVE_SYNC, msg);
        }
    }

    public void addInteractiveRecord(InteractiveRecord interactiveRecord, BigInteger effectValue){
        if(null == interactiveRecord){
            return;
        }
        synchronized (interactiveRecordList){
            if(interactiveRecordList.size() >= GameConfig.INTERACTIVE_RECORD_MAX_COUNT){
                interactiveRecordList.removeLast();
            }
            interactiveRecordList.addFirst(interactiveRecord);
        }
        //通知被点赞或者蛊惑
        player.sendPacket(Protocol.U_INTERACTIVE_NOTICE, PlayerBasePb.parseInteractiveNoticeMsg(interactiveRecord, effectValue));
    }

    public UserInteractive getUserInteractive() {
        return userInteractive;
    }

    public List<InteractiveRecord> getInteractiveRecordList(){
        return new ArrayList<>(interactiveRecordList);
    }

    public void addOpLikeUserId(long userId){
        synchronized (currLikeUserIdList) {
            if(!isOpLikeUser(userId)) {
                currLikeUserIdList.add(userId);
            }
        }
    }

    public void removeOpLikeUserId(long userId){
        synchronized (currLikeUserIdList) {
            if(isOpLikeUser(userId)) {
                currLikeUserIdList.remove(userId);
            }
        }
    }

    public boolean isOpLikeUser(long userId){
        return currLikeUserIdList.contains(userId);
    }

    public void addOpHateUserId(long userId){
        synchronized (currHateUserIdList) {
            if (!isOpHateUser(userId)) {
                currHateUserIdList.add(userId);
            }
        }
    }

    public void removeOpHateUserId(long userId){
        synchronized (currHateUserIdList) {
            if (isOpHateUser(userId)) {
                currHateUserIdList.remove(userId);
            }
        }
    }

    public boolean isOpHateUser(long userId){
        return currHateUserIdList.contains(userId);
    }
}
